package com.hegaoye.core.exceptions;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * 基本异常类
 *
 * @author lixin 2017-08-03 17:46
 */
public class BaseException extends RuntimeException implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(BaseException.class);

    public BaseException(BaseExceptionEnum exceptionMessage) {
        super(exceptionMessage.toString());
        logger.error("系统发生异常[{}]", exceptionMessage.toString());
    }

    public BaseException(BaseExceptionEnum exceptionMessage, Object... params) {
        super(exceptionMessage.toString());
        logger.error("系统发生异常[{}],参数为[{}]", exceptionMessage.toString(), JSON.toJSONString(params));
    }




    public BaseException(String message) {
        super(message);
        logger.error("系统发生异常[{}]", message);
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
        logger.error("系统发生异常[{}],异常为[{}]", message, cause);
    }


    /*异常信息定义:按照功能严格定义，对于格式验证，参数验证等，基本所有接口都会用到，统一定义在公用异常定义区间*/
    public enum BaseExceptionEnum {
        //成功
        Success("0000", "Success"),
        //-----------------公用异常定义 [0001~0500]--------------------------
        Password_Short("0001", "Password is too short"),
        Email_Incorrect("0002", "Incorrect email format"),
        phone_Incorrect("0003", "Incorrect phone format"),
        Length_short("0004", "Length is too short"),
        Illegal_Param("0006", "Parameter is illegal"),
        Empty_Param("0007", "Parameter is empty"),
        Exists("0010", "There is already exists"),
        Result_Not_Exist("0011", "The query result does not exist"),
        Must_Be_In_English("0015", "must be in english"),
        Account_Or_Password_Error("0016", "account or password is error"),

        //-----------------系统异常定义 [9000~9499]--------------------------
        Server_Error("9999", "Server Error"),
        Session_Error("9001", "Session is not exists"),

        //-----------------员工异常定义[1000~1050]--------------------------
        JobNumber_Exist("1000", "jobNumber is exist"),
        JobNumber_Illegal("1001", "jobNumber is illegal"),
        Staff_Leave("1002", "Staff is leave"),

        //-----------------数据同步异常定义[1051~1100]--------------------------
        No_Data_To_Update("1051", "No data to update"),//没有数据需要更新

        //-----------------账户异常定义[1101~1150]--------------------------

        //-----------------工厂异常定义[1151~1200]--------------------------

        //-----------------维修异常定义[1201~1300]--------------------------

        //-----------------样本异常定义[1301~1400]--------------------------

        //-----------------序列号异常定义[1301~1350]--------------------------

        //-----------------供应商异常定义[1351~1450]--------------------------

        //-----------------测试异常定义[1451~1550]----------------------------

        //-----------------bom异常定义[1551~1700]----------------------------
        Mould_Is_Stable("1551", "mould is stable, it is not allow update"),//mould已经是稳定状态，不允许被修改
        Mould_Is_Stop("1552", "mould is stop, it is not allow update"),//mould已经是停用状态，不允许被修改
        Assembly_Category_Name_Exist("1553", "assembly category name is already exist"),//组件类型名称已存在
        Assembly_State_Is_Stop("1554", "assembly name is stop"),//组件状态已是stop，不允许修改
        Assembly_State_Is_Stable("1555", "assembly name is stable"),//组件状态Stable，不允许修改
        State_Is_Stop("1556", "state is stop"),//组件状态已是stop，不允许修改
        State_Is_Stable("1557", "state is stable"),//组件状态Stable，不允许修改
        State_Is_Not_Stable("1558", "state is not stable"),//状态不是稳定状态
        Material_Is_Not_Enable("1559", "state is not enable"),//状态不是可用状态
        Bom_Not_Develop("1560", "bom  state is not develop"),
        Assembly_Category_Exist("1561", "assembly category is already exist"),//组件类型名称已存在
        Assembly_Category_Not_Exist("1562", "assembly category is not exist"),//组件类型不存在
        Assembly_Exist("1563", "assembly is already exist"),//组件已存在
        Father_Assembly_Not_Exist("1564", "The parent assembly information does not exist"),//上级组件信息不存在
        Father_Bom_Not_Exist("1565", "The parent bom information does not exist"),//上级bom信息不存在
        Bom_Name_Is_Already_Exist("1566", " bom name is already exist"),//bom名称已存在
        Bom_Code_Is_Already_Exist("1567", " bom code is already exist"),//bom代码
        Bom_has_been_copy("1568", "bom has been copy"),//bom已经复制
        Assembly_Not_Exist("1569", "assembly is not exist"),//组件信息不存在
        Product_Sku_Exist("1570", "product sku is already exist"),//成品sku信息已存在
        Product_Upc_Exist("1571", "product upc is already exist"),//成品upc信息已存在
        Product_Has_Been_Copy("1572", "product has been copy"),//成品已经被复制
        Project_Category_name_Is_Exist("1573", "project category name is already exists"),//项目分类名称已经存在
        Project_Category_Is_Not_Exist("1574", "project category  is not exists"),//项目分类不存在
        Project_Name_Is_Exist("1575", "project name is already exists"),//项目名称已经存在
        Project_Code_Is_Exist("1576", "project code is already exists"),//项目代码已经存在
        ;

        public String errCode;//错误编码
        public String error;//错误信息

        BaseExceptionEnum(String errCode, String error) {
            this.errCode = errCode;
            this.error = error;
        }

        @Override
        public String toString() {
            return "{code:\"" + errCode + "\", error:\"" + error + "\"}";
        }

    }
}
